package com.hj.ct.fragment;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.ValueFormatter;
import com.hj.ct.R;
import com.hj.ct.activity.LineChartActivity;
import com.hj.ct.logic.LogicActions;
import com.hj.ct.parse.GatherData;
import com.hj.ct.util.DateUtil;

public class LineChartFragment extends BaseFragment {

	private LineChart mChart;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_linechart, null);
		mChart = (LineChart) view.findViewById(R.id.chart1);

		mChart.setDescription("粉尘噪声曲线图");
		mChart.setNoDataTextDescription("暂无数据");

		mChart.setHighlightEnabled(false);
		mChart.setTouchEnabled(true);
		mChart.setDrawGridBackground(false);
		mChart.setPinchZoom(true);
		// setData(10, 180);
		mChart.animateY(1000);

		Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Regular.ttf");

		XAxis xAxis = mChart.getXAxis();
		xAxis.setPosition(XAxisPosition.BOTTOM);
		xAxis.setTypeface(tf);
		xAxis.setTextSize(12f);
		xAxis.setTextColor(Color.BLACK);
		xAxis.setDrawGridLines(false);
		xAxis.setSpaceBetweenLabels(1);

		YAxis leftAxis = mChart.getAxisLeft();
		leftAxis.setTypeface(tf);
		leftAxis.setStartAtZero(false);
		leftAxis.setTextColor(ColorTemplate.getHoloBlue());
		leftAxis.setDrawGridLines(true);

		YAxis rightAxis = mChart.getAxisRight();
		rightAxis.setTypeface(tf);
		rightAxis.setTextColor(Color.RED);
		rightAxis.setStartAtZero(false);
		rightAxis.setAxisMinValue(0);
		rightAxis.setDrawGridLines(false);
		return view;
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		if (mChart != null) {
			mChart.animateY(1000);
		}
	}

	public void handlerUI(int action, int code, Object... obj) {
		if (action == LogicActions.HISTORY_DATA_SUCCESS) {
			List<GatherData> datas = (List<GatherData>) obj[0];
			showData(datas);
		} else if (action == LogicActions.NEED_TO_LOGIN) {
			showReLoginDialog();
		} else if (action == LogicActions.HISTORY_DATA_ERROR) {
			toast((String) obj[0]);
		}
	}

	public void showData(List<GatherData> list) {
		ArrayList<String> xVals = new ArrayList<String>();// x轴数据
		ArrayList<Entry> yVals1 = new ArrayList<Entry>();// 噪声
		ArrayList<Entry> yVals2 = new ArrayList<Entry>();// 粉尘
		float dustMax = 0;
		float noiseMax = 0;
		float dustMin = 10000;
		float noiseMin = 10000;
		int dur = ((LineChartActivity) getActivity()).getDur();
		for (int i = 0, size = list.size(); i < size; i++) {
			GatherData data = list.get(i);
			xVals.add(DateUtil.formatResponseTime(data.getRecTime(), dur));
			float n = Float.parseFloat(data.getLAFMaxPrf());
			float d = Float.parseFloat(data.getDustInst());
			if (n > noiseMax) {
				noiseMax = n;
			}
			if (d > dustMax) {
				dustMax = d;
			}
			if (n < noiseMin) {
				noiseMin = n;
			}
			if (d < dustMin) {
				dustMin = d;
			}
			yVals1.add(new Entry(n, i));
			yVals2.add(new Entry(d, i));
		}

		// create a dataset and give it a type
		LineDataSet set1 = new LineDataSet(yVals1, "噪声 ");
		set1.setAxisDependency(AxisDependency.LEFT);
		set1.setColor(ColorTemplate.getHoloBlue());
		set1.setDrawCircles(true);
		set1.setCircleColor(ColorTemplate.getHoloBlue());
		set1.setLineWidth(2f);
		set1.setValueTextColor(ColorTemplate.getHoloBlue());

		// create a dataset and give it a type
		LineDataSet set2 = new LineDataSet(yVals2, "粉尘");
		set2.setAxisDependency(AxisDependency.RIGHT);
		set2.setColor(Color.RED);
		set2.setCircleColor(Color.RED);
		set2.setLineWidth(2f);
		set2.setDrawCircles(true);
		set2.setValueTextColor(Color.RED);
		// set3.setDrawCubic(true);

		ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
		dataSets.add(set1); // add the datasets
		dataSets.add(set2);

		// create a data object with the datasets
		LineData data = new LineData(xVals, dataSets);
		data.setValueTextColor(Color.BLACK);
		data.setValueTextSize(9f);
		data.setValueFormatter(new ValueFormatter() {
			DecimalFormat fm = new DecimalFormat("0.000");
			public String getFormattedValue(float value) {
				return fm.format(value);
			}
		});
		data.setDrawValues(true);

		mChart.getAxisLeft().setAxisMaxValue(noiseMax+1);
		mChart.getAxisLeft().setAxisMinValue(noiseMin);
		mChart.getAxisRight().setAxisMaxValue(dustMax+0.1f);
		mChart.getAxisRight().setAxisMinValue(dustMin-0.1f);
		mChart.getAxisRight().setValueFormatter(new ValueFormatter() {
			DecimalFormat fm = new DecimalFormat("0.000");
			public String getFormattedValue(float value) {
				return fm.format(value);
			}
		});
		// set data
		mChart.setData(data);
		mChart.notifyDataSetChanged();
		mChart.invalidate();
	}

}
